package com.example.asus.yuejian_10.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.activity.ShaiTuInfoActivity;
import com.example.asus.yuejian_10.bean.UserShaitu;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/10/10 0010.
 */

public class UserShaiTuAdapter extends RecyclerView.Adapter {


    private List<UserShaitu> userShaituList;
    private Context context;
    private LayoutInflater layoutInflater;
    private String name,pic;

    public UserShaiTuAdapter(Context context,List<UserShaitu> userShaituList,String name,String pic){
        this.context=context;
        this.userShaituList=userShaituList;
        this.name=name;
        this.pic=pic;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView=LayoutInflater.from(context).inflate(R.layout.item_user_shaitu,viewGroup,false);
        UserShaiTuViewHolder holder=new UserShaiTuViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        UserShaiTuViewHolder userShaiTuViewHolder= (UserShaiTuViewHolder) viewHolder;
        final UserShaitu shaitu=userShaituList.get(position);
        String title=shaitu.getTitle();
        String time=shaitu.getUpdate_time();
        String photo= Config.PIC_URL+shaitu.getThumb0();
        String photo1= Config.PIC_URL+shaitu.getThumb1();
        String photo2= Config.PIC_URL+shaitu.getThumb2();
        userShaiTuViewHolder.u_title.setText(title);
        userShaiTuViewHolder.u_time.setText(time);
        userShaiTuViewHolder.u_name.setText(name);
        if (pic==null){
            Picasso.with(context).load(R.drawable.bg_1).into(userShaiTuViewHolder.u_pic);
        }else {
        Picasso.with(context).load(Config.PIC_URL+pic).into(userShaiTuViewHolder.u_pic);
        }

        if (!shaitu.getThumb0().equals("")&&!shaitu.getThumb0().equals("a")){
        Picasso.with(context).load(photo).into(userShaiTuViewHolder.u_shaitu);
        }else {
            Picasso.with(context).load(R.drawable.bg_1).into(userShaiTuViewHolder.u_shaitu);
        }
        if (!shaitu.getThumb1().equals("")) {
            Picasso.with(context).load(photo1).into(userShaiTuViewHolder.u_shaitu1);
        }else {
            Picasso.with(context).load(R.drawable.bg_1).into(userShaiTuViewHolder.u_shaitu1);
        }
        if (!shaitu.getThumb2().equals("")) {
            Picasso.with(context).load(photo2).into(userShaiTuViewHolder.u_shaitu2);
        }else {
            Picasso.with(context).load(R.drawable.bg_1).into(userShaiTuViewHolder.u_shaitu2);
        }

        userShaiTuViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, ShaiTuInfoActivity.class);
                intent.putExtra("ShaiTu_Id",shaitu.getId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return userShaituList.size();
    }


    public class UserShaiTuViewHolder extends RecyclerView.ViewHolder{


        public ImageView u_shaitu;
        public ImageView u_shaitu1;
        public ImageView u_shaitu2;
        public ImageView u_pic;
        public TextView u_name;
        public TextView u_time;
        public TextView u_title;
        public View itemView;

        public UserShaiTuViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            u_name= (TextView) itemView.findViewById(R.id.user_shaitu_name);
            u_time= (TextView) itemView.findViewById(R.id.user_shaitu_time);
            u_title= (TextView) itemView.findViewById(R.id.user_shaitu_title);
            u_pic= (ImageView) itemView.findViewById(R.id.user_shaitu_pic);
            u_shaitu= (ImageView) itemView.findViewById(R.id.user_shaitu_photo);
            u_shaitu1= (ImageView) itemView.findViewById(R.id.user_shaitu_photo1);
            u_shaitu2= (ImageView) itemView.findViewById(R.id.user_shaitu_photo2);
        }
    }
}
